package com.yunban.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.DecoderMapper;
import net.lab1024.sa.base.EventEnum;
import com.yunban.project.template.model.domain.Decoder;
import com.yunban.project.template.model.dto.DecoderReq;
import com.yunban.project.template.model.dto.DecoderSearchReq;
import com.yunban.project.template.model.vo.DecoderResp;
import com.yunban.project.template.service.DecoderService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author: lijiajun
 * @date: 2024-11-07
 * @version: 1.0
 */
@Service
public class DecoderServiceImpl extends ServiceImpl<DecoderMapper, Decoder> implements DecoderService {
    @Override
    public Decoder findById(long id) {
        Decoder r = this.getById(id);
        if (ObjectUtils.isEmpty(r)) {
            throw new BusinessException("该数据不存在");
        }
        return r;
    }

    @Override
    public PageResult<DecoderResp> getDecoderList(DecoderSearchReq req) {
        LambdaQueryWrapper<Decoder> wrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(req.getName())) {
            wrapper.eq(Decoder::getName, req.getName());
        }
        if (null != req.getState()) {
            wrapper.eq(Decoder::isState, req.getState());
        }
        return SmartPageUtil.convert2PageResult(this.page(SmartPageUtil.convert2PageQuery(req), wrapper), DecoderResp.class);
    }

    long getUserId() {
        Long userId = SmartRequestUtil.getRequestUserId();
        if (null == userId) {
            userId = 0L;
        }
        return userId;
    }

    @Override
    public Boolean saveDecoder(DecoderReq req) {
        Decoder decoder = new Decoder();
        BeanUtils.copyProperties(req, decoder);
        // 自动写入抽取脚本执行顺序
        decoder.setExecuteOrder((int) this.count() + 1);
        decoder.setCreator(getUserId());
        return this.save(decoder);
    }

    @Override
    public Boolean editDecoder(long id, DecoderReq req) {
        Decoder decoder = this.findById(id);
        BeanUtils.copyProperties(req, decoder);
        decoder.setUpdateCreator(getUserId());
        return this.updateById(decoder);
    }

    @Override
    public Boolean updateState(long id, EventEnum event) {
        Decoder decoder = this.findById(id);
        switch (event) {
            case enable:
                decoder.setState(true);
                break;
            case disable:
                decoder.setState(false);
                break;
            default:
                throw new BusinessException("参数错误");
        }
        decoder.setUpdateCreator(getUserId());
        return this.updateById(decoder);
    }

    @Override
    public Boolean delDecoder(long id) {
        throw new BusinessException("暂不支持删除");
    }
}